<?php
class Model_MapperCategorie implements Model_IMapper
{
	private $table = null;
	
	public function __construct($str_table, $pdo)
	{
		$this->table = new Model_TableCategorie();
		$this->table->setConnexion($pdo);
		$this->table->init();
	}
	
	public function getAll()
	{
		$results = array();
		foreach($this->table->fetchAll() as $ligne)
		{
			$entity = new Model_Categorie();
			$entity->setId_categorie($ligne['id_categorie']);
			$entity->setNom($ligne['nom']);
			$results[] = $entity;
		}
		return $results;
	}
	
	public function getOne($id)
	{
		$ligne = $this->table->fetchOne($id);
		if(!empty($ligne['id_categorie']) )
		{
			$entity = new Model_Categorie();
			$entity->setId_categorie($ligne['id_categorie']);
			$entity->setNom($ligne['nom']);
		}
		else 
		{
			$entity = null;
		}
		return $entity;
	}
	
	public function save(Model_IEntity $object)
	{
		if($object->getId_categorie() == -1)
		{
			$id = $this->table->insert($object->getNom());
			$object->setId_categorie($id);
		}
		else
		{
			$this->table->update($object->getId_categorie(), $object->getNom());
		}
	}
	
	public function delete(Model_IEntity $object)
	{
		$this->table->delete($object->getId_categorie());
	}
}